import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
import { FunnelChart, Funnel, LabelList, Tooltip, ResponsiveContainer, Cell } from 'recharts';

const PanelContainer = styled.div`
  height: 300px;
  background: rgba(10, 25, 41, 0.7);
  border: 1px solid rgba(0, 255, 255, 0.3);
  border-radius: 5px;
  padding: 15px;
  position: relative;
  overflow: hidden;
  
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, rgba(0, 255, 255, 0.8), transparent);
  }
  
  &:after {
    content: '';
    position: absolute;
    bottom: 0;
    right: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.8));
  }
`;

const PanelHeader = styled.div`
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  position: relative;
  
  &:after {
    content: '';
    position: absolute;
    bottom: -6px;
    left: 0;
    width: 100%;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.5), transparent);
  }
`;

const PanelTitle = styled.div`
  font-size: 18px;
  font-weight: bold;
  color: #fff;
`;

const CustomTooltip = styled.div`
  background: rgba(10, 25, 41, 0.9);
  border: 1px solid rgba(0, 255, 255, 0.5);
  border-radius: 4px;
  padding: 8px;
  color: #fff;
  font-size: 12px;
`;

interface FunnelChartPanelProps {
  title?: string;
}

const FunnelChartPanel: React.FC<FunnelChartPanelProps> = ({ title = "资源占比趋势" }) => {
  const COLORS = ['#8884d8', '#83a6ed', '#8dd1e1', '#82ca9d', '#a4de6c'];
  
  const [data, setData] = useState([
    { name: '资源获取', value: 100, percentage: '100%' },
    { name: '资源分配', value: 80, percentage: '80%' },
    { name: '资源处理', value: 60, percentage: '60%' },
    { name: '资源优化', value: 40, percentage: '40%' },
    { name: '资源输出', value: 20, percentage: '20%' }
  ]);
  
  // 模拟数据更新
  useEffect(() => {
    const timer = setInterval(() => {
      const newData = [...data];
      // 更新第一个值（总量）
      newData[0] = {
        ...newData[0],
        value: Math.max(50, Math.min(150, newData[0].value + Math.floor(Math.random() * 20) - 10))
      };
      
      // 基于第一个值计算其他值的百分比
      for (let i = 1; i < newData.length; i++) {
        const percentage = Math.max(10, Math.min(95, Math.floor(Math.random() * (105 - i * 15)) - 5 + (100 - i * 15)));
        const prevValue = newData[i-1].value;
        const value = Math.floor(prevValue * percentage / 100);
        
        newData[i] = {
          ...newData[i],
          value,
          percentage: `${percentage}%`
        };
      }
      
      setData(newData);
    }, 3000);
    
    return () => clearInterval(timer);
  }, [data]);
  
  const renderCustomTooltip = ({ active, payload }: any) => {
    if (active && payload && payload.length) {
      const data = payload[0].payload;
      return (
        <CustomTooltip>
          <p style={{ margin: '0 0 5px' }}><strong>{data.name}</strong></p>
          <p style={{ margin: '0' }}>{`数值: ${data.value}`}</p>
          <p style={{ margin: '0' }}>{`占比: ${data.percentage}`}</p>
        </CustomTooltip>
      );
    }
    return null;
  };

  return (
    <PanelContainer>
      <PanelHeader>
        <PanelTitle>{title}</PanelTitle>
      </PanelHeader>
      <ResponsiveContainer width="100%" height="90%">
        <FunnelChart>
          <Tooltip content={renderCustomTooltip} />
          <Funnel
            dataKey="value"
            data={data}
            isAnimationActive={true}
            width={300}
            height={220}
          >
            {data.map((entry, index) => (
              <Cell 
                key={`cell-${index}`} 
                fill={COLORS[index % COLORS.length]} 
                stroke="rgba(0, 255, 255, 0.3)"
                strokeWidth={1}
              />
            ))}
            <LabelList 
              position="right" 
              fill="#fff" 
              stroke="none" 
              dataKey="name" 
              fontSize={11}
            />
            <LabelList 
              position="center" 
              fill="#fff" 
              stroke="none" 
              dataKey="percentage" 
              fontSize={10}
            />
          </Funnel>
        </FunnelChart>
      </ResponsiveContainer>
    </PanelContainer>
  );
};

export default FunnelChartPanel; 